package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.Page;
import com.kaishengit.util.RowMapper;


public class DocumentDao {
	
	private DBHelp<Document> db=new DBHelp<Document>();
	
	public Long getCount(){
		String sql="select count(*) from t_document";
		return (Long)db.executeQueryForFunction(sql);
		}
	
	public Page<Document> findAll(Page<Document> page,String projectid){
		String sql="select * from t_document where t_project_id=? limit ?,?";
		List<Document> list= db.executeQueryForList(sql,new DocumentRowMapper(),projectid,page.getOffSet(),page.getPageSize());
		page.setResult(list);
		return page;
	}
	
	public void save(Document d){
		final String sql="insert into t_document(id,title,content,createtime,t_project_id,t_user_id)values(?,?,?,?,?,?)";
		db.executeSQL(sql,d.getId(),d.getTitle(),d.getContent(),d.getCreatetime(),d.getProject().getId(),d.getUser().getId());
		
	}

	public List<Document> findByProjectId(String projectId) {
		StringBuilder sql=new StringBuilder();
		sql.append("SELECT td.id,td.title,td.content,td.createtime,t_project_id,t_user_id,tu.`password`,tu.`name` as username,tp.`name`,tp.`desc`,tp.createtime as time FROM t_document AS td ");
		sql.append("LEFT JOIN t_user AS tu ON td.t_user_id=tu.id ");
		sql.append("LEFT JOIN t_project AS tp ON td.t_project_id=tp.id ");
		sql.append("WHERE td.t_project_id=? ");
		return db.executeQueryForList(sql.toString(),new DocumentRowMapper(),projectId);
	}
		public Document findById(String documentId){
			StringBuilder sql=new StringBuilder();
			sql.append("SELECT td.id,td.title,td.content,td.createtime,t_project_id,t_user_id,tu.`password`,tu.`name` as username,tp.`name`,tp.`desc`,tp.createtime as time FROM t_document AS td ");
			sql.append("LEFT JOIN t_user AS tu ON td.t_user_id=tu.id ");
			sql.append("LEFT JOIN t_project AS tp ON td.t_project_id=tp.id ");
			sql.append("WHERE td.id=? ");
			return db.executeQueryForObject(sql.toString(), new DocumentRowMapper(), documentId);
		
	}

	private class DocumentRowMapper implements RowMapper<Document>{

		public Document mapperRow(ResultSet rs) throws SQLException {
			Document d=new Document();
			d.setId(rs.getString("id"));
			d.setContent(rs.getString("content"));
			d.setCreatetime(rs.getString("createtime"));
			d.setStringtime(rs.getString("createtime"));
			d.setTitle(rs.getString("title"));
			
			User user=new User();
			user = new UserDao().findById(rs.getString("t_user_id"));
			d.setUser(user);
			d.setUserid(rs.getString("t_user_id"));
			
			Project project=new Project();
			project = new ProjectDao().findById(rs.getString("t_project_id"));
			d.setProject(project);
			d.setProjectid(rs.getString("t_project_id"));
			
			return d;
			
		}
	}

	public void update(String id, String title, String content) {
		String sql="UPDATE t_document SET title=?,content=? WHERE id=?";
		db.executeSQL(sql, title,content,id);
	}

	public void del(String id) {
		String sql="delete from t_document where id=?";
		db.executeSQL(sql, id);
	}

	
}
